package org.tgcms.core.dal.appengine.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

import org.tgcms.core.dal.dto.Attachment;
import org.tgcms.core.dal.dto.Page;
import org.tgcms.core.dal.dto.Template;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.datanucleus.annotations.Unowned;

//@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
//@FetchGroup(name = "all", members = { @Persistent(name = "attributes"),
//		@Persistent(name = "attachments"), @Persistent(name = "template") }) //, @Persistent(name = "website") })
@PersistenceCapable(detachable="true")
//@Version(strategy = VersionStrategy.VERSION_NUMBER, column = "version", extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
//@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
@Inheritance(customStrategy = "complete-table")
@Version(strategy = VersionStrategy.VERSION_NUMBER, column = "version", extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
//@FetchGroup(name = "all", members = { @Persistent(name = "parent") })

public class AppenginePage implements AppengineEntity, Page {

	@PrimaryKey
	private Key key;

	private Long version;

	@Persistent
	private String url;

	@Persistent
	private String title;

	@Persistent
	private Text content;

	@Persistent
	private Date date;

	@Persistent
	private Boolean draft;

	@Persistent
	@Unowned
	private AppengineTemplate template;

	@Persistent
	private Map<String, String> attributes;

	@Persistent
	@Element(dependent = "true")
	private List<AppengineAttachment> attachments;

	public AppenginePage(Key key) {
		this.key = key;
	}
	
	public AppenginePage(Page page) {
		this.url = page.getUrl();
		this.title = page.getTitle();
		if(page.getContent() != null) {
			this.content = new Text(page.getContent());
		}
		this.date = page.getDate();
		this.draft = page.isDraft();
		if(page.getTemplate() instanceof AppengineTemplate) {
			this.template = (AppengineTemplate) page.getTemplate();
		}
		this.attributes = page.getAttributes();
		this.attachments = new ArrayList<AppengineAttachment>();
		for(Attachment attachment : page.getAttachments()) {
			if(attachment instanceof AppengineAttachment) {
				this.attachments.add((AppengineAttachment) attachment);
			}
		}
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return (content == null)?null:content.getValue();
	}

	public void setContent(String content) {
		this.content = new Text(content);
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Boolean isDraft() {
		return draft;
	}

	public void setDraft(Boolean draft) {
		this.draft = draft;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		if(template instanceof AppengineTemplate) {
			this.template = (AppengineTemplate) template;
		}
	}

	public Map<String, String> getAttributes() {
		if(attributes== null) {
			attributes = new HashMap<String, String>();
		}
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	@SuppressWarnings("unchecked")
	public List<Attachment> getAttachments() {
		if(attachments == null) {
			attachments = new ArrayList<AppengineAttachment>();
		}
		return (List<Attachment>)(List<?>) attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
//		this.attachments = attachments;
		throw new RuntimeException("Not implemented");
	}
	
}
